package be.vdab.Servlets;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import be.vdab.dao.ReadSqlDAO;
import be.vdab.entities.Member;

/**
 * Servlet implementation class BevestigenServlet
 */
@WebServlet("/bevestigen.htm")
public class BevestigenServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String VIEW = "/WEB-INF/JSP/bevestigen.jsp";
	private ReadSqlDAO sqlDAO = new ReadSqlDAO();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		session.removeAttribute("fout");
		if (session.getAttribute("member") != null){
			Member member = (Member) session.getAttribute("member");
			String gebruiker = member.getVoornaam() + " "
					+ member.getFamilienaam() + " " + member.getStraat()
					+ " " + member.getHuisNr() + " " + member.getPostcode()
					+ " " + member.getGemeente();
			session.setAttribute("gebruiker", gebruiker);
		}
		
		if (session.getAttribute("aangemeld") == null){
			if (!request.getParameterMap().isEmpty()) {
				
				try {
					String naam = request.getParameter("gebruikersNaam");
					String paswoord = request.getParameter("paswoord");
	
					Member member = sqlDAO.getMember(naam, paswoord);
					session.setAttribute("member", member);
	
					String gebruiker = member.getVoornaam() + " "
							+ member.getFamilienaam() + " " + member.getStraat()
							+ " " + member.getHuisNr() + " " + member.getPostcode()
							+ " " + member.getGemeente();
					session.setAttribute("gebruiker", gebruiker);
					request.setAttribute("disabled", "disabled");
					request.setAttribute("disabledBevestiging", null);
					
					session.setAttribute("aangemeld","aangemeld");
	
				} catch (Exception ex) {
					String gebruiker = "Verkeerde gebruikersnaam of paswoord";
					request.setAttribute("gebruiker", gebruiker);
					request.setAttribute("disabled", null);
					request.setAttribute("disabledBevestiging", "disabled");
				}
			} else {
				request.setAttribute("disabledBevestiging", "disabled");
			}
		} else {
			request.setAttribute("disabled", "disabled");
			request.setAttribute("disabledBevestiging", null);
		}
			
		

		RequestDispatcher dispatcher = request.getRequestDispatcher(VIEW);
		dispatcher.forward(request, response);
	}

}
